﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lib.Bases;
using Lib.Utils;

namespace MS.INV.Costs
{
    public partial class ChiPhi_New : Lib.Bases.Forms.FBaseEdit
    {
        SysObj.Cp_HoatDong cls = new SysObj.Cp_HoatDong();

        public ChiPhi_New(int _ObjID)
        {
            InitializeComponent();

            this.ObjID = _ObjID;
            this._BaseClass = cls;

            btOther1.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
            btOther1.Caption = "Loại chi phí";
            btOther1.ImageIndex = 7;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            
            LoadList();

            try
            {
                if (this.ObjID > 0)
                {
                    cls.IdChiPhi = this.ObjID;
                    cls.ReadData();

                    txtDG.Text = cls.DienGiai;
                    txtSo.Text = cls.So;
                    dtNgay.EditValue = cls.Ngay;

                    _grid.DataSource = cls.GetDetail();
                }
                else T_NewItemClick(null, null);
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Form load.");
            }
        }

        private void LoadList()
        {
            using (SysLst.Cp_Loai cp = new SysLst.Cp_Loai())
                repLoai.SetDataSource(cp.LoadData(), "TenLoai#Tên loại");
        }

        public override void T_NewItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.T_NewItemClick(sender, e);
            _grid.DataSource = cls.GetDetail();
            txtSo.Text = cls.GetNo(Convert.ToDateTime(dtNgay.EditValue));
        }

        public override void T_UpdateItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (txtSo.Text.Trim().Length <= 0)
                {
                    Msg.ErrOK("Vui lòng nhập số chứng từ.");
                    return;
                }
                if (object.Equals(dtNgay.EditValue, null))
                {
                    Msg.ErrOK("Vui lòng chọn ngày lập chứng từ.");
                    return;
                }

                cls.So = txtSo.Text;
                cls.Ngay = Convert.ToDateTime(dtNgay.EditValue);
                cls.DienGiai = txtDG.Text;

                cls.Detail = _grid.GetAcceptChanges();

                base.T_UpdateItemClick(sender, e);
            }
            catch (Exception ex)
            {
                SysError.RaiseError(ex, this.Name, "Update data");
            }
        }

        public override void T_DelItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                base.T_DelItemClick(sender, e);
            }
            catch (Exception ex)
            {
                SysError.RaiseError(ex, this.Name, "Delete data");
            }
        }

        private void dtNgay_EditValueChanged(object sender, EventArgs e)
        {
            if (cls.IdChiPhi <= 0)
            {
                txtSo.Text = cls.GetNo(Convert.ToDateTime(dtNgay.EditValue));
            }
        }

        public override void T_Other1ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Lib.Bases.Forms.FBaseList lst = new Lib.Bases.Forms.FBaseList();
            lst.StartPosition = FormStartPosition.CenterScreen;
            lst.ShowInTaskbar = false;
            lst.BaseList = new SysLst.Cp_Loai();
            lst.ShowDialog();

            if (lst.SaveChanged)
            {
                LoadList();
            }
        }
    }
}
